package satir.framework.graph;

import javax.swing.JFrame;
import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.JPanel;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.SpiderWebPlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RefineryUtilities;

/**
 * The Class Generator.
 * @version 3.0 April 2012
 * @author Creative Family
 */
public class Generator extends JFrame {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = 1L;

    /** The person list. */
    private ArrayList<Person> personList;

    /** The chart title. */
    private String chartTitle;

    /**
     * Instantiates a new generator.
     * @param applicationTitle
     *            the application title
     * @param chartTitle
     *            the chart title
     */
    public Generator(final String applicationTitle, final String chartTitle) {
	super(applicationTitle);
	this.chartTitle = chartTitle;
	personList = new ArrayList<Person>();
    }

    /**
     * Creates the dataset.
     * @return the category dataset
     */
    private CategoryDataset createDataset() {
	Person player;
	ArrayList<Data> dataList;
	DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
	for (int i = 0; i < personList.size(); i++) {
	    player = personList.get(i);
	    dataList = player.getDataList();
	    for (int j = 0; j < dataList.size(); j++) {
		defaultcategorydataset.addValue(dataList.get(j).getValue(),
			player.getName(), dataList.get(j).getLabel());
	    }
	}
	return defaultcategorydataset;
    }

    /**
     * Creates the chart.
     * @param categorydataset
     *            the categorydataset
     * @return the j free chart
     */
    private JFreeChart createChart(final CategoryDataset categorydataset) {
	SpiderWebPlot spiderwebplot = new SpiderWebPlot(categorydataset);
	spiderwebplot.setStartAngle(54D);
	spiderwebplot.setInteriorGap(0.40000000000000002D);
	spiderwebplot
		.setToolTipGenerator(new StandardCategoryToolTipGenerator());
	JFreeChart jfreechart = new JFreeChart(chartTitle,
		TextTitle.DEFAULT_FONT, spiderwebplot, false);
	LegendTitle legendtitle = new LegendTitle(spiderwebplot);
	legendtitle.setPosition(RectangleEdge.BOTTOM);
	jfreechart.addSubtitle(legendtitle);
	return jfreechart;
    }

    /**
     * Creates the demo panel.
     * @return the j panel
     */
    public JPanel createDemoPanel() {
	JFreeChart jfreechart = createChart(createDataset());
	return new ChartPanel(jfreechart);
    }

    /**
     * Adds the person.
     * @param player
     *            the player
     */
    public void addPerson(final Person player) {
	personList.add(player);
    }

    /**
     * Sets the char title.
     * @param chartTitle
     *            the new char title
     */
    public void setCharTitle(final String chartTitle) {
	this.chartTitle = chartTitle;
    }

    /**
     * Generate.
     */
    public void generate() {
	this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	JPanel jpanel = createDemoPanel();
	jpanel.setPreferredSize(new Dimension(500, 500));
	setContentPane(jpanel);
	this.pack();
	RefineryUtilities.centerFrameOnScreen(this);
	this.setVisible(true);
    }
}
